//
//  ViewController.m
//  PickerViewForNationalFlag
//
//  Created by 移云 on 2019/11/11.
//  Copyright © 2019 MaoYeYeDeJiangCuCha. All rights reserved.
//

#import "ViewController.h"
#import <Masonry/Masonry.h>
#import "FlagModel.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)NSArray *flagArray;//国旗

@property(nonatomic,weak)UILabel* countryLabel;
@property(nonatomic,weak)UIPickerView* nationalPickerView;
@end

@implementation ViewController

#pragma mark - 懒加载
- (NSArray *)flagArray{
    
    if (_flagArray == nil) {
        
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"03flags.plist" ofType:nil]];
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictArr.count];
        for (NSDictionary* dict in dictArr) {
            FlagModel *model = [[FlagModel alloc]initWithDict:dict];
            [arrayM addObject:model];
        }
        _flagArray = arrayM;
    }
    
    return _flagArray;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setupUI];
    //默认选中
    [self pickerView:self.nationalPickerView didSelectRow:0 inComponent:0];
}


- (void)setupUI{
    
    
    UIPickerView *nationalPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 80, UIScreen.mainScreen.bounds.size.width, 200)];
    self.nationalPickerView = nationalPickerView;
    nationalPickerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:nationalPickerView];
    nationalPickerView.delegate = self;
    nationalPickerView.dataSource = self;
    
    UILabel *countryLabel = [[UILabel alloc]init];
    self.countryLabel = countryLabel;
    countryLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:countryLabel];
    [countryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(nationalPickerView.mas_bottom).offset(40);
    }];
    
    
}

#pragma mark - UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 80)];
    cellView.backgroundColor = [UIColor whiteColor];
    UILabel *countryLabel = [[UILabel alloc]init];
    [cellView addSubview:countryLabel];
    [countryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.equalTo(cellView.mas_centerY);
    }];
    UIImageView *flagImageView = [[UIImageView alloc]init];
    [cellView addSubview:flagImageView];
    [flagImageView mas_makeConstraints:^(MASConstraintMaker *make) {         make.edges.equalTo(cellView).with.insets(UIEdgeInsetsMake(10, UIScreen.mainScreen.bounds.size.width - 100,10,10));
    }];

    FlagModel *model = self.flagArray[row];
    countryLabel.text = model.name;
    flagImageView.image = [UIImage imageNamed:model.icon];
//    NSString *path = [[NSBundle mainBundle] pathForResource:model.icon ofType:nil];
//    flagImageView.image =  [UIImage imageWithContentsOfFile:path];

    return cellView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 80;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    FlagModel *model = self.flagArray[row];
    self.countryLabel.text =  model.name;
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.flagArray.count;
}






@end
